package GamePoker.PokerJava;

import java.util.Scanner;

public class GameProgressFour {
    Scanner sc=new Scanner(System.in);
    PokerWonder text=new PokerWonder();
    public int DownPayment;
    private int check;
    SponsorShip total=new SponsorShip();
    private int MinHaveCopy;//赌注最少的人
    private int AllinCheck=0;//Allin人数
    private int DicardCount=0;//弃牌人数
    public void GamethreePepole(Player p1,Player p2,Player p3,Player p4)
    {
        total.total=DownPayment*4;
        p1.money.PokerMoney-=DownPayment;
        p2.money.PokerMoney-=DownPayment;
        p3.money.PokerMoney-=DownPayment;
        p4.money.PokerMoney-=DownPayment;
        int MinHave=Math.min(Math.min(p1.money.PokerMoney,p2.money.PokerMoney),Math.min(p3.money.PokerMoney,p4.money.PokerMoney));
        MinHaveCopy=MinHave;
        for (int i = 0; DicardCount<3&&AllinCheck<4; i++) {
            System.out.println("输入1进行投注");
            System.out.println("输入2进行Allin");
            System.out.println("输入3进行弃牌");
            if(!p1.PlayerDiscard)
            {
                if(DicardCount==3)
                {
                    break;
                }
                if(p1.PlayerQT&&DicardCount==2)
                {
                    break;
                }
                System.out.println("下面请"+p1.playerName+"进行选择:");
                if(p2.PlayerQT||p3.PlayerQT||p4.PlayerQT)
                {
                    System.out.println("因为前面该玩家选择ALLIN所以你必须选择Allin或者弃牌,不能输入1");
                    while(true)
                    {
                        check=sc.nextInt();
                        if(check==1){
                            System.out.println("输入错误请重新输入");}
                        else {
                            break;
                        }
                    }
                    OnlyPlayChoice(p1);
                }
                else {
                    PlayChoice(p1);
                }
                if(AllinCheck==4&&(AllinCheck==2&&DicardCount==2)&&DicardCount==3)
                {
                    break;
                }
            }
            if(!p2.PlayerDiscard)
            {
                if(DicardCount==3)
                {
                    break;
                }
                if(p2.PlayerQT&&DicardCount==2)
                {
                    break;
                }
                System.out.println("下面请"+p2.playerName+"进行选择:");
                if(p1.PlayerQT||p3.PlayerQT||p4.PlayerQT) {
                    System.out.println("因为前面该玩家选择ALLIN所以你必须选择Allin或者弃牌,不能输入1");
                    while (true) {
                        check = sc.nextInt();
                        if(check==1){
                            System.out.println("输入错误请重新输入");}
                        else {
                            break;
                        }
                    }
                    OnlyPlayChoice(p2);
                }
                else {
                    PlayChoice(p2);
                }
                if(AllinCheck==4&&(AllinCheck==2&&DicardCount==2)&&DicardCount==3)
                {
                    break;
                }
            }
            if(!p3.PlayerDiscard)
            {
                if(DicardCount==3)
                {
                    break;
                }
                if(p3.PlayerQT&&DicardCount==2)
                {
                    break;
                }
                System.out.println("下面请"+p3.playerName+"进行选择:");
                if(p1.PlayerQT||p2.PlayerQT||p4.PlayerQT) {
                    System.out.println("因为前面该玩家选择ALLIN所以你必须选择Allin或者弃牌,不能输入1");
                    while (true) {
                        check = sc.nextInt();
                        if(check==1){
                            System.out.println("输入错误请重新输入");}
                        else {
                            break;
                        }
                    }
                    OnlyPlayChoice(p3);
                }
                else {
                    PlayChoice(p3);
                }
                if(AllinCheck==4&&(AllinCheck==2&&DicardCount==2)&&DicardCount==3)
                {
                    break;
                }
            }
            if(!p4.PlayerDiscard) {
                if(DicardCount==3)
                {
                    break;
                }
                if(p4.PlayerQT&&DicardCount==2)
                {
                    break;
                }
                System.out.println("下面请"+p4.playerName+"进行选择:");
                if (p1.PlayerQT || p2.PlayerQT || p3.PlayerQT) {
                    System.out.println("因为前面该玩家选择ALLIN所以你必须选择Allin或者弃牌,不能输入1");
                    while (true) {
                        check = sc.nextInt();
                        if(check==1){
                            System.out.println("输入错误请重新输入");}
                        else {
                            break;
                        }
                    }
                    OnlyPlayChoice(p4);
                } else {
                    PlayChoice(p4);
                }
                if(AllinCheck==4&&(AllinCheck==2&&DicardCount==2)&&DicardCount==3)
                {
                    break;
                }
            }
            check=0;
            if(AllinCheck==3)
            {
                break;
            }
            System.out.println("本轮弃牌人数为"+DicardCount+"人");//可以写个谁弃权
            System.out.println("继续游戏");
        }
    }
    public void OnlyPlayChoice(Player p)
    {
        if(check==2)
        {
            if(MinHaveCopy>=p.money.PokerMoney)
            {
                total.total+=p.money.PokerMoney;
                p.money.PokerMoney=0;
            }
            else {
                total.total+=MinHaveCopy;
                p.money.PokerMoney-=MinHaveCopy;
            }
            AllinCheck++;
            p.PlayerQT=true;
        }
        if(check==3)
        {
            text.Discard();
            DicardCount++;
            p.PlayerDiscard=true;
        }
    }
    public void PlayChoice(Player p)
    {
        check=sc.nextInt();
        if(check==1)
        {
            System.out.println("当前资金为:"+p.money.PokerMoney);
            int changeMoney;
            text.Bet();
            while(true){
                changeMoney=sc.nextInt();
                if(changeMoney<=p.money.PokerMoney)
                {
                    System.out.println("投注成功");
                    break;
                }
                else {
                    System.out.println("投注金额大于自己拥有的金额");
                }
            }
            total.total+=changeMoney;
            p.money.PokerMoney-=changeMoney;
        }
        if(check==2)
        {
            text.AllIn();
            if(MinHaveCopy>=p.money.PokerMoney)
            {
                total.total+=p.money.PokerMoney;
                p.money.PokerMoney=0;
            }
            else {
                total.total+=MinHaveCopy;
                p.money.PokerMoney-=MinHaveCopy;
            }
            AllinCheck++;
            p.PlayerQT=true;
        }
        if(check==3)
        {
            text.Discard();
            DicardCount++;
            p.PlayerDiscard=true;
        }
        check=0;
    }

    public void PokerCompareThree(Player c1, Player c2, Player c3,Player c4) {
        if(DicardCount==0)
        {
            if(c1.pokerObj.PokerCompare(c2.pokerObj))
            {
                if(c1.pokerObj.PokerCompare(c3.pokerObj))
                {
                    if(c1.pokerObj.PokerCompare(c4.pokerObj))
                    {
                        c1.WIN=true;
                        System.out.println(c1.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }
                }
                else {
                    if(c3.pokerObj.PokerCompare(c4.pokerObj)){
                        c3.WIN=true;
                        System.out.println(c3.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }

                }
            }
            else{
                if(c2.pokerObj.PokerCompare(c3.pokerObj))
                {
                    if(c2.pokerObj.PokerCompare(c4.pokerObj)){
                        c2.WIN=true;
                        System.out.println(c2.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }

                }
                else {
                    if(c3.pokerObj.PokerCompare(c4.pokerObj))
                    {
                        c3.WIN=true;
                        System.out.println(c3.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }
                }
            }
        }
        if(DicardCount==1)
        {
            if(c1.PlayerDiscard)
            {
                if(c2.pokerObj.PokerCompare(c3.pokerObj))
                {
                    if(c2.pokerObj.PokerCompare(c4.pokerObj))
                    {
                        c2.WIN=true;
                        System.out.println(c2.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }
                }
                else{
                    if(c3.pokerObj.PokerCompare(c4.pokerObj))
                    {
                        c3.WIN=true;
                        System.out.println(c3.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }
                }
            }
            if(c2.PlayerDiscard)
            {
                if(c1.pokerObj.PokerCompare(c3.pokerObj))
                {
                    if(c1.pokerObj.PokerCompare(c4.pokerObj))
                    {
                        c1.WIN=true;
                        System.out.println(c1.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }
                }
                else{
                    if(c3.pokerObj.PokerCompare(c4.pokerObj))
                    {
                        c3.WIN=true;
                        System.out.println(c3.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }
                }
            }
            if(c3.PlayerDiscard)
            {
                if(c1.pokerObj.PokerCompare(c2.pokerObj))
                {
                    if(c1.pokerObj.PokerCompare(c4.pokerObj))
                    {
                        c1.WIN=true;
                        System.out.println(c1.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }
                }
                else{
                    if(c2.pokerObj.PokerCompare(c4.pokerObj))
                    {
                        c2.WIN=true;
                        System.out.println(c2.playerName+"的牌更大");
                    }
                    else {
                        c4.WIN=true;
                        System.out.println(c4.playerName+"的牌更大");
                    }
                }
            }
            if(c4.PlayerDiscard)
            {
                if(c1.pokerObj.PokerCompare(c2.pokerObj))
                {
                    if(c1.pokerObj.PokerCompare(c3.pokerObj))
                    {
                        c1.WIN=true;
                        System.out.println(c1.playerName+"的牌更大");
                    }
                    else {
                        c3.WIN=true;
                        System.out.println(c3.playerName+"的牌更大");
                    }
                }
                else{
                    if(c2.pokerObj.PokerCompare(c3.pokerObj))
                    {
                        c2.WIN=true;
                        System.out.println(c2.playerName+"的牌更大");
                    }
                    else {
                        c3.WIN=true;
                        System.out.println(c3.playerName+"的牌更大");
                    }
                }
            }
        }
        if(DicardCount==2)
        {
            if(c1.PlayerDiscard&& c2.PlayerQT)
            {
                if(c3.pokerObj.PokerCompare(c4.pokerObj))
                {
                    c3.WIN=true;
                    System.out.println(c3.playerName+"的牌更大");
                }
                else {
                    c4.WIN=true;
                    System.out.println(c4.playerName+"的牌更大");
                }
            }
            if(c2.PlayerDiscard&&c3.PlayerDiscard)
            {
                if(c1.pokerObj.PokerCompare(c4.pokerObj))
                {
                    c1.WIN=true;
                    System.out.println(c1.playerName+"的牌更大");
                }
                else
                {
                    c4.WIN=true;
                    System.out.println(c4.playerName+"的牌更大");
                }
            }
            if(c3.PlayerDiscard&&c4.PlayerDiscard)
            {
                if(c1.pokerObj.PokerCompare(c2.pokerObj))
                {
                    c1.WIN=true;
                    System.out.println(c1.playerName+"的牌更大");
                }
                else {
                    c2.WIN=true;
                    System.out.println(c2.playerName+"的牌更大");
                }
            }
        }
        if(DicardCount==3)
        {
            if(c1.PlayerDiscard&& c2.PlayerDiscard&&c4.PlayerDiscard)
            {
                c3.WIN=true;
                System.out.println("其他玩家放弃比赛，"+c3.playerName+"获得全部资金");
            }
            if(c1.PlayerDiscard&& c3.PlayerDiscard&&c4.PlayerDiscard)
            {
                c2.WIN=true;
                System.out.println("其他玩家放弃比赛，"+c2.playerName+"获得全部资金");
            }
            if(c2.PlayerDiscard&& c3.PlayerDiscard&&c4.PlayerDiscard)
            {
                c1.WIN=true;
                System.out.println("其他玩家放弃比赛，"+c1.playerName+"获得全部资金");
            }
        }
    }
}


